package com.powerManager.sdk.web;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowCallbackHandler;

import com.powerManager.sdk.common.JdbcSingleton;


public class InitAppPathMap {

	private static Map<String, String[]> PATH_MAP = new HashMap<String, String[]>();
	
	private static final String GENERAL_VALUE="com.powerManager.sdk.handlers.GeneralHandler";
	
	private static final String GENERAL_METHOD="execute";
	
	public InitAppPathMap(){
		reloadPath();
	}
	
	public static void reloadPath(){
		//PATH_MAP.clear();
		PATH_MAP = new HashMap<String, String[]>();
		//PATH_MAP.put("/general", new String[]{GENERAL_VALUE,GENERAL_METHOD,"a"});
		JdbcTemplate jdbc = JdbcSingleton.getJdbcTemplate();
		String sql = "select PID,PKey,PValue,PTarget,PType from sys_path where PStatus=1";
		PATH_MAP = jdbc.query(sql, new ResultSetExtractor<Map<String, String[]>>(){

			public Map<String, String[]> extractData(ResultSet rs)
					throws SQLException, DataAccessException {
				Map<String, String[]> data = new HashMap<String, String[]>();
				while(rs.next()){
					String[] col = new String[5];
					col[0]=rs.getString("PValue");
					col[1]=rs.getString("PTarget");
					col[2]=rs.getString("PID");
					col[3]=rs.getString("PType");
					String key = rs.getString("PKey");
					System.out.println(key+"\t\t\t"+col[1]+"\t\t\t"+col[0]);
					data.put(key, col);
				}
				return data;
			}
			
		});
		//ModuleDAO module = new ModuleDAO();
		//PATH_MAP = module.getValid();
		
	}
	
	public static void add(String key,String[] value){
		PATH_MAP.put(key, value);
	}
	
	public static void update(String key,String[] value){
		remove(key);
		PATH_MAP.put(key, value);
	}
	
	public static void remove(String key){
		PATH_MAP.remove(key);
	}
	
	public static String[] get(String key){
		return PATH_MAP.get(key);
	}
}
